(async function() {
    const resp = await API.profile();
    const user = resp.data;
    console.log(user)
    if (!user) {
        alert('未登录，请先登录，确认跳转登录页面');
        location.href = './login.html'
        return
    }
    const doms = {
            aside: {
                nickname: $('#nickname'),
                loginId: $('#loginId'),
            },
            close: $('.close'),
            chatContainer: $('.chat-container'),
            textMsginput: $('#txtMsg'),
            msgfrom: $('.msg-container')
        }
        // console.log(doms.aside.nickname)
    doms.aside.loginId.innerText = user.loginId;
    doms.aside.nickname.innerText = user.nickname;
    //注销登录
    doms.close.onclick = function() {
        API.removelLogin();
        location.href = './login.html'
    }


    function addChat(chatInfo) {
        const div = $$$('div')
        div.className = 'chat-item'
        if (chatInfo.from) {
            div.classList.add('me')
        }
        const img = $$$('img')
        img.className = 'chat-avatar'
        img.src = chatInfo.from ? './asset/avatar.png' : './asset/robot-avatar.jpg'
        const content = $$$('div');
        content.className = 'chat-content'
        content.innerText = chatInfo.content;
        const chatdate = $$$('div');
        chatdate.className = 'chat-date'
        chatdate.innerText = dateTime(chatInfo.createdAt)
        div.appendChild(img);
        div.appendChild(content);;
        div.appendChild(chatdate);
        div.appendChild(chatdate);
        doms.chatContainer.appendChild(div)
    }


    function dateTime(time) {
        const date = new Date(time)
        const year = date.getFullYear();
        const month = (date.getMonth() + 1).toString().padStart(2, '0')
        const day = (date.getDate()).toString().padStart(2, '0')
        const hours = (date.getHours()).toString().padStart(2, '0')
        const minutes = (date.getMinutes()).toString().padStart(2, '0')
        const seconds = (date.getSeconds()).toString().padStart(2, '0')

        return `${year}-${month}-${day}  ${hours}:${minutes}:${seconds}`


    }
    loginHis();

    async function loginHis() {
        const resp = await API.getHistory();
        console.log(resp)
        for (const item of resp.data) {
            addChat(item)
        };
        scrolltop();
    }


    function scrolltop() {
        doms.chatContainer.scrollTop = doms.chatContainer.scrollHeight;

    }

    async function sendChat() {
        const content = doms.textMsginput.value.trim();
        if (!content) {
            return
        }
        addChat({
            from: user.loginId,
            to: null,
            content: content,
            createdAt: Date.now()
        })
        scrolltop();
        doms.textMsginput.value = ''
        const resp = await API.sendChat(content);
        addChat({
            from: null,
            to: user.loginId,
            ...resp.data
        })
        scrolltop()
    }
    doms.msgfrom.onsubmit = function(e) {
        e.preventDefault();
        sendChat();
    }
})()